package com.klikmee.sms.core.base.communicator.starion;

import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.log4j.Logger;

import com.klikmee.sms.cef.bulk.EventParams;
import com.klikmee.sms.cef.bulk.HttpBulkSender;
import com.klikmee.sms.core.commons.events.AppEvent;
import com.klikmee.sms.core.commons.events.Dispatcher;

public class StarionBulkSenderImpl extends HttpBulkSender{
	/**
	 * Logger instance
	 */
	private Logger		logger	= Logger.getLogger(StarionBulkSenderImpl.class);

	/**
	 * Dispatcher instance
	 */
	private Dispatcher	dispatcher;

	/**
	 * Enumeration of possible http parameters applicable to Startion bulk
	 * provider
	 */
	public enum StarionHttpParamNames {
		SEQ("seq"), USERNAME("username"), MESSAGE("message"), DEST("dest"), SOURCE("source"), TIME("time"), MSGTYPE("msg_type"), DELIVERYREPORT("dr");
		// string representation of the enum element
		private String	name	= null;

		// private constructor for enum elements
		private StarionHttpParamNames(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return this.name;
		}
	}

	@Override
	protected void afterBulkMessageSent(String responseBody, Hashtable<BulkParams, Object> params) {
		
	}

	@Override
	public NameValuePair[] createQueryStringParams(Hashtable<BulkParams, Object> params) {
		// init array with the length of hash values
		NameValuePair[] result = new NameValuePair[params.keySet().size()];
		// iterate over generic params and translate them to startion params and
		// values
		Enumeration<BulkParams> keys = params.keys();
		// index of the next empry array element
		int arrayCounter = 0;
		while (keys.hasMoreElements()) {
			BulkParams param = keys.nextElement();
			switch (param) {
			case SENDER:
				result[arrayCounter] = new NameValuePair(StarionHttpParamNames.SOURCE.toString(), params.get(param).toString());
				break;
			case RECEIVERS:
				result[arrayCounter] = new NameValuePair(StarionHttpParamNames.DEST.toString(), params.get(param).toString());
				break;
			case TEXT:
				result[arrayCounter] = new NameValuePair(StarionHttpParamNames.MESSAGE.toString(), params.get(param).toString());
				break;
				
			case SEQ_ID:
				result[arrayCounter] = new NameValuePair(StarionHttpParamNames.SEQ.toString(), params.get(param).toString());
				break;
			case USERNAME:
				result[arrayCounter] = new NameValuePair(StarionHttpParamNames.USERNAME.toString(), params.get(param).toString());
				break;
			}
				
			// increment counter
			arrayCounter++;
		}
		return result;
	}

	public Dispatcher getDispatcher() {
		return dispatcher;
	}

	public void setDispatcher(Dispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}
}
